package com.kaili.basic.controller;


import com.kaili.basic.pojo.dto.BaseDictAddDto;
import com.kaili.basic.pojo.dto.BaseDictEditDto;
import com.kaili.basic.pojo.dto.BaseDictTypeAddDto;
import com.kaili.basic.pojo.entity.BaseDict;
import com.kaili.basic.pojo.vo.BaseDictTypeVO;
import com.kaili.basic.pojo.vo.BaseDictVO;
import com.kaili.basic.service.IBaseDictService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.CommonUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author nigang
 * @since 2018-05-04
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "数据字典", description = "数据字典相关API")
public class BaseDictController {
    @Autowired
    IBaseDictService baseDictService;

    @PostMapping(value = "dicts/")
    @ApiOperation(value = "添加数据字典", notes = "添加数据字典")
    public RestStatus addDict(@Validated @RequestBody BaseDictAddDto dictDto) {
        BaseDict dict=new BaseDict();
        BeanUtils.copyProperties(dictDto,dict);
        Long loginUserId = LoginDetailUtils.getCurrentEmployeeId().longValue();
        dict.setCreatedBy(loginUserId);
        dict.setCreatedTime(new Date());
        if(dict.getParentId()==null){
            dict.setParentId(0);
        }

        Integer dictOrder=baseDictService.getMaxDictOrder(dict.getParentId());
        if(dictOrder==null){
            dictOrder=1;
        }else{
            dictOrder++;
        }
        dict.setDictOrder(dictOrder);

        if(baseDictService.getDictByCode(dict).size()>0){
            return CommonStatus.DICT_CODE_EXIST;
        }
        if(baseDictService.getDictByName(dict).size()>0){
            return CommonStatus.DICT_NAME_EXIST;
        }
        if (baseDictService.addDict(dict) > 0) {
            return CommonStatus.ADD_OK;
        }
        return CommonStatus.ADD_FAIL;
    }

    @PostMapping(value = "dicts/addDictsType")
    @ApiOperation(value = "添加数据字典类型", notes = "添加数据字典类型")
    public RestStatus addDictType(@Validated @RequestBody BaseDictTypeAddDto dictDto) {
        BaseDict dict=new BaseDict();
        dict.setDictName(dictDto.getDictName());
        Long loginUserId = LoginDetailUtils.getCurrentEmployeeId().longValue();
        dict.setDictCode("/");
        dict.setCreatedBy(loginUserId);
        dict.setCreatedTime(new Date());
        if(dict.getParentId()==null){
            dict.setParentId(0);
        }

        Integer dictOrder=baseDictService.getMaxDictTypeOrder();
        if(dictOrder==null){
            dictOrder=1;
        }else{
            dictOrder++;
        }
        dict.setDictOrder(dictOrder);

        if(baseDictService.getDictByName(dict).size()>0){
            return CommonStatus.DICT_TYPE_NAME_EXIST;
        }
        if (baseDictService.addDict(dict) > 0) {
            return CommonStatus.ADD_OK;
        }
        return CommonStatus.ADD_FAIL;
    }

    @PutMapping(value = "dicts/dictTypeOrder")
    @ApiOperation(value = "数据字典类型排序", notes = "修改数据字典类型的顺序")
    public Map<String, Object> dictTypeOrder(@RequestBody List<Integer> list){
        int num=1;
        for(Integer dictId:list){
            BaseDict dict=new BaseDict();
            dict.setDictId(dictId);
            dict.setDictOrder(num);
            baseDictService.updateDict(dict);
            num++;
        }
        return null;
    }

    @DeleteMapping(value = "dicts/{dictId}")
    @ApiOperation(value = "删除数据字典", notes = "根据id删除数据字典并删除其子项")
    public RestStatus deleteDict(@PathVariable("dictId") Integer dictId){
        baseDictService.deleteDictById(dictId);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping(value = "dicts/")
    @ApiOperation(value = "修改数据字典", notes = "修改数据字典")
    public RestStatus updateDict(@Validated @RequestBody BaseDictEditDto editDto) {
        BaseDict dict=new BaseDict();
        BeanUtils.copyProperties(editDto,dict);
        Long loginUserId = LoginDetailUtils.getCurrentEmployeeId().longValue();

        if(baseDictService.getDictByIdCode(dict).size() == 0) {
            if (baseDictService.getDictByCode(dict).size() > 0) {
                return CommonStatus.DICT_CODE_EXIST;
            }
        }

        if(baseDictService.getDictByIdName(dict).size() == 0) {
            if (baseDictService.getDictByName(dict).size() > 0) {
                return CommonStatus.DICT_NAME_EXIST;
            }
        }

        dict.setModifiedBy(loginUserId);
        dict.setModifiedTime(new Date());
        if (baseDictService.updateDict(dict) > 0) {
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.UPDATE_FAIL;
    }

    @GetMapping(value = "dicts/{dictId}/autoDictCode")
    @ApiOperation(value = "获取自增编号", notes = "通过字典类型id获取最大的数据字典id来封装自增编号")
    public String getAutoDictCode(@PathVariable("dictId") Integer dictId){
        Integer dictCode=baseDictService.getMaxDictOrder(dictId);
        if(dictCode==null){
            dictCode=1;
        }else{
            dictCode++;
        }
        return StringUtils.leftPad(dictCode.toString(), 2, "0");
    }

    @GetMapping(value = "dicts/{parentName}/byParentName")
    @ApiOperation(value = "查子项数据", notes = "根据字典类型名称查询其子项数据")
    public List<BaseDict> getDictByParentName(@PathVariable("parentName")String parentName){
        return baseDictService.getDictByParentName(parentName);
    }

    @GetMapping(value = "dicts/allDictType")
    @ApiOperation(value = "查所有字典类型", notes = "查所有字典类型")
    public List<BaseDictTypeVO> getAllDictType(){
        return CommonUtil.toList(baseDictService.getAllDictType(), BaseDictTypeVO.class);
    }

    @GetMapping(value = "/dicts/{dictId}/byDictId")
    @ApiOperation(value = "查字典", notes = "根据id查询数据字典")
    public BaseDictVO getDictById(@PathVariable("dictId") Integer dictId){
        return baseDictService.getDictById(dictId);
    }

    @GetMapping(value = "dicts/{parentId}/byParentId")
    @ApiOperation(value = "查子项数据", notes = "根据id查询其子项数据")
    public List<BaseDict> getDictByParentId(@PathVariable("parentId") Integer parentId){
        return baseDictService.getDictByParentId(parentId);
    }

    @GetMapping(value = "/dicts/{dictId}/relationDict")
    @ApiOperation(value = "根据id查询有关联的数据字典", notes = "根据id查询有关联的数据字典")
    public List<BaseDict> getDictByDictId(@PathVariable("dictId") Integer dictId){
        return baseDictService.getDictByDictId(dictId);
    }

    @PostMapping(value = "dicts/dictByDictId2")
    public List<BaseDict> getDictByDictId2(@RequestBody BaseDict dict){
        return baseDictService.getDictByDictId2(dict.getDictId());
    }

    @RequestMapping(value = "/serviceGet/{name}", method = RequestMethod.GET)
    public String dictService(@PathVariable("name") String name) {
        return baseDictService.selectByDictName(name).getDictId().toString();
    }
}

